import { createStore } from 'vuex'

const store= createStore({
    state(){
        return{
            tabMap:new Map(),
            activeName:{},
            active:false,
            prevTab:{},
        }
    },
    mutations:{
        addTabs:(state, payload) => {
            state.active= !state.active;
            state.activeName = payload.id;
            state.tabMap.set(payload.id,payload);
        },
        removeTab:(state,payload)=>{
            state.active= !state.active;
            const  newMap = new Map();
            state.tabMap.forEach((ele,index)=>{
                if(payload == ele.id){
                    if(payload == state.activeName){
                        state.activeName = state.prevTab.id;
                    }
                }else{
                    newMap.set(ele.id,ele);
                    state.prevTab = ele;
                }
            })
            state.tabMap = newMap;
        },
        active:(params,payload)=>{
            console.log("===============",params,payload);
        }
        },
    getters:{
        loadTabs(tab){
            let tabs = [];
            store.state.tabMap.forEach((ele,index)=>{
                tabs.push({id:ele.id,title: ele.title,name:ele.id,path:ele.path,icon:ele.icon})
            })
            return tabs;
        },
        tabActive(){
            return store.state.activeName;
        },
        tabActivePath(){
            let key = store.state.activeName;
            return store.state.tabMap.get(key);
        }
    },
    actions:{
        registerTab:({commit}, payload,active) => {

            commit('addTabs',payload);
        }
    }
})

export default store
